1408B - Arrays Sum - CodeForces Solution


constructive algorithms greedy math *1400

Please click on ads to support us..

Python Code:

import sys
import math


MOD = 1000000007

def solve():
    pass

for _ in range(int(input())):
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    c = len(set(a))

    if k >= c: print(1)
    elif k == 1: print(-1)
    else:
        print(math.ceil((c-1)/(k-1)))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi(i, m, n) for (ll i = m; i < n; i++)
#define fd(i, n, m) for (ll i = n; i >= m; i--)
const int mod = 1e9 + 7;
const double e =1e-7;

void solve(){
    int n,k;
    cin >> n >> k;
    unordered_set <int> s;
    vector <int> a(n);
    for(int i=0;i<n;++i){
        cin >> a[i];
        s.insert(a[i]);
    }
    int n_d = s.size();
    if(n_d > 1 && k ==1){cout << -1 << endl;}
    else if (n_d==1 && k==1){cout << 1 << endl;}
    else {cout << (max(0,(n_d-2))/(k-1)+1)<<endl;}

}

int main()
{
    int t;
    cin >> t;
    while (t--) solve();
}


Comments

Submit
0 Comments
More Questions

1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings